<?php

namespace app\admin\controller;

use app\admin\model\RecoreModel;
use app\admin\model\RoomModel;
use app\admin\model\StudentModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;
use think\Request;

class RecordController extends AdminBaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $name = input('name');
        $record = Db::name('record')
            ->join('cmf_student','cmf_record.stu_id=cmf_student.id')
            ->join('cmf_score','cmf_record.score_id=cmf_score.id')
            ->join('cmf_reason','cmf_record.reason_id=cmf_reason.id')
            ->where(function (Query $query)use ($name){
                $query->where('cmf_student.name','like',"%$name%");
            })
            ->field('cmf_student.name as student_name,
            cmf_record.*,cmf_score.name as score_name,
            cmf_reason.name as reason_name')
            ->order('id DESC')
            ->paginate(3,false,['query'=>$this->request->param()]);
        $page = $record->render();
        return view('index',['record'=>$record,'page'=>$page]);

    }

    public function add(){
        if (request()->isPost()){
            $data = input();
            unset($data['photo_name']);
            $add = Db::name('record')->insert($data);
            if ($add) {
                $this->success('添加成功', 'record/index');
                $this->error('添加失败');
            }
        }
        $reason = Db::name('reason')->select();
        $school = Db::name('school')->select();
        $score = Db::name('score')->select();
        return view('add',['reason'=>$reason,'school'=>$school,'score'=>$score]);
    }
    public function getRoombySchool()
    {
        //
        $schoolId = input('school_id');//获取当前school_id
        //halt($schoolId);
        $room = RoomModel::where(['sid'=>$schoolId])->select();
        //查询班级列表中的school_id
        if ($room) {
            $option = ["<option value=''>--请选择班级--</option>"];//默认学校值
            foreach ($room as $v) {//循环
                $opt = "<option value='" . $v['id'] . "'>" . $v['name'] . "</option>";
                array_push($option, $opt);
            }
            return $option;
        } else {
            return "<option value=''>暂无数据</option>";
        }
    }
    public function getRoombyStudent()
    {
        //
        $roomId = input('room_id');//获取当前school_id
        $student = StudentModel::where(['cid'=>$roomId])->select();
        //查询班级列表中的school_id
        if ($student) {
            $option = ["<option value=''>--请选择学生--</option>"];//默认学校值
            foreach ($student as $v) {//循环
                $opt = "<option value='" . $v['id'] . "'>" . $v['name'] . "</option>";
                array_push($option, $opt);
            }
            return $option;
        } else {
            return "<option value=''>暂无数据</option>";
        }
    }

    public function edit($id)
    {
        if(request()->isPost()){
            $data = input();
            unset($data['photo_name']);
            $edit = Db::name('record')->update($data);
            if($edit)
                $this->success('修改成功','record/index');
            $this->error('修改失败');
        }
        $record = Db::name('record')->get($id);
        $reason = Db::name('reason')->select();
        $score = Db::name('score')->select();
        $school = Db::name('school')->select();
        $studentId = $record['stu_id'];
        $student = Db::name('student')
            ->join('cmf_room','cmf_student.cid=cmf_room.id')
            ->join('cmf_school','cmf_room.sid=cmf_school.id')
            ->where('cmf_student.id',$studentId)
            ->field('cmf_room.id as room_id,cmf_school.id as school_id,cmf_student.id as student_id')
            ->select();
        $student=$student[0];
        $school_id = $student['school_id'];
        $record['school_id'] = $school_id;
        $room_id = $student['room_id'];
        $record['room_id']=$room_id;
        $room = Db::name('room')->where('sid',$student['school_id'])->select();
        $student = Db::name('student')->where('cid',$student['room_id'])->select();
        return $this->fetch('edit',[
            'student'=>$student,
            'reason'=>$reason,
            'score'=>$score,
            'record'=>$record,
            'school'=>$school,
            'room'=>$room
        ]);
    }


    public function delete($id)
    {
        RecordModel::destroy($id);
    }
    public function delAll(){
        $ids = input('ids/a');
        $del = RecordModel::destroy($ids);
        if ($del){
            $this->success('删除成功','recore/index');
        }$this->error('删除失败');
    }
}
